//
//  ViewController.m
//  TextMe
//
//  Created by qianfeng on 16/5/30.
//  Copyright © 2016年 zhaoshaoqing. All rights reserved.
//

#import "ViewController.h"
#import "XMLDictionary.h"
#import "ZSQHttpmanager.h"
#import "AFNetworking.h"


@interface ViewController ()<UIWebViewDelegate>

@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,strong) NSArray *images;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//        NSDictionary *params = @{@"p":@"a",@"c":@"6140756089165492331",@"u":@"1",@"net":@"wifi",@"cdma_lng":@"113.669790",@"cdma_lat":@"34.773578",@"cdma_acc":@"39.000000",@"cdma_sTime":@"1464695109429",@"v":@"5.6.0",@"h":@"3336",@"t":@"1464695333187",@"gbcode":@"410100"};
    
        [[ZSQHttpmanager sharedManager] POST:@"http://api.haodou.com/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1465281056537&vc=84&vn=6.1.1&loguid=0&deviceid=haodou862421021258566&uuid=22e20322a9662d91ec2f72e2c29a7e76&channel=360_v611&method=Album.info&virtual=&signmethod=md5&v=3&timestamp=1465281573&nonce=0.6833184629650111&appsign=8ebb7a33d7cce21824ea3f38e34ce179" param:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"%@",responseObject);
    
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];

    
    
    
    /**
    self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    _webView.delegate = self;
    [self.view addSubview:self.webView];
    
    
    NSURL *fileUrl = [[NSBundle mainBundle]URLForResource:@"index" withExtension:@"html"];
    
    
    // 3.加载请求（即将文件展示到webView上）
//    NSURLRequest *request = [NSURLRequest requestWithURL:fileUrl];
    
//    NSURL *URL = [[NSURL alloc] initWithString:@"http://baidu.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:fileUrl];
    [_webView loadRequest:request];
    NSURL *URL = [[NSURL alloc] initWithString:@"http://zcache.k.sohu.com/api/news/cdn/v1/article.go/136280651/0/613/0/3/1/12/34/3/1/1/1464332623012.xml"];
    NSString *stringxml = [[NSString alloc] initWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:NULL];
    
//    NSLog(@"%@",stringxml);
    NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:stringxml];
    //
//    NSLog(@"xmlDoc: %@", xmlDoc);
    
    //    NSLog(@"xmlDoc: %@", xmlDoc);
    NSString *title = xmlDoc[@"title"];
    NSString *time = xmlDoc[@"time"];
    NSString *content = xmlDoc[@"content"];
    NSString *fromnews = xmlDoc[@"from"];
    NSArray *photoS = xmlDoc[@"photos"][@"photo"];
    NSArray *photoArray = xmlDoc[@"images"][@"image"];
    
    self.images = photoArray;
    
    NSMutableString *muContentString = [NSMutableString stringWithString:content];
    if (self.images.count > 0) {
        for (int i = 0; i < self.images.count; i++) {
            NSString *replaceContent = [NSString stringWithFormat:@"<p><image_%d></image_%d></p>",i,i];
            NSString *withContent = [NSString stringWithFormat:@"<p><img src='%@'/></p>",self.images[i]];
            [muContentString replaceOccurrencesOfString:replaceContent withString:withContent options:NSCaseInsensitiveSearch range:NSMakeRange(0, muContentString.length - 1)];
        }
//        NSLog(@"%@",muContentString);
    }
    
    
    
    
    [_webView loadHTMLString:muContentString baseURL:nil];
    
    
    [self reload];
    ***********/
    
    


//    NSDictionary *params = @{@"p":@"a",@"c":@"6140756089165492331",@"u":@"1",@"net":@"wifi",@"cdma_lng":@"113.669790",@"cdma_lat":@"34.773578",@"cdma_acc":@"39.000000",@"cdma_sTime":@"1464695109429",@"v":@"5.6.0",@"h":@"3336",@"t":@"1464695333187",@"gbcode":@"410100"};
//    
//    [[ZSQHttpmanager sharedManager] POST:@"http://api.k.sohu.com/api/channel/v5/news.go" param:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSLog(@"%@",responseObject);
//
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"%@",error);
//    }];
    
//   [[ZSQHttpmanager sharedManager] GETXML:@"http://zcache.k.sohu.com/api/news/cdn/v1/article.go/136958305/0/613/0/3/1/12/34/3/1/1/1464499646008.xml" param:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//       NSLog(@"%@",responseObject);
//   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//       NSLog(@"%@",error);
//   }];
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
//    [manager GET:@"http://zcache.k.sohu.com/api/news/cdn/v1/article.go/136958305/0/613/0/3/1/12/34/3/1/1/1464499646008.xml" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSLog(@"%@",responseObject);
//        
////        NSDictionary *xmlDoc =  [[XMLDictionaryParser sharedInstance] dictionaryWithData:data];
////        NSLog(@"%@",xmlDoc);
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"%@",error);
//    }];
    
//    NSURL *URL = [[NSURL alloc] initWithString:@"http://zcache.k.sohu.com/api/news/cdn/v1/article.go/136958305/0/613/0/3/1/12/34/3/1/1/1464499646008.xml"];
//    NSString *string = [[NSString alloc] initWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:NULL];
//
//    NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:string];
//    
//    NSLog(@"xmlDoc: %@", xmlDoc);
////    NSLog(@"%@",xmlDoc[@"photos"][@"photo"]);
//    NSArray *array = xmlDoc[@"photos"][@"photo"];
//    for (NSDictionary *dict in array) {
////        NSLog(@"%@",dict);
//        NSLog(@"%@",dict[@"pic"]);
//        NSString *pic = dict[@"pic"];
//        NSInteger width = [dict[@"height"] integerValue];
//        NSInteger height = [dict[@"width"] integerValue];
//#define VIEWWIDTH self.view.bounds.size.width
//        CGFloat viewHeight = height/(CGFloat)width * VIEWWIDTH;
//    }
}


/**
 *  刷新WebView页面
 */
- (void)reload{
    [self.webView reload];
}
/**
 *  停止加载WebView页面
 */
- (void)stopLoading{
    [self.webView stopLoading];
}
/**
 *  后退到上一个WebView页面
 */
- (void)goBack{
    // webView提供判断可后退的方法
    if ([self.webView canGoBack]) {
        [self.webView goBack];
    }
}
/**
 *  再返回到后退前的WebView页面
 */
- (void)goForward{
    // webView提供判断可前进的方法
    if ([self.webView canGoForward]) {
        [self.webView goForward];
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}








//- (void)subcontentString:(NSString *)content Height:(CGFloat)height{
//    NSString *temp = @"";
//    NSString *strContent = @"";
//    CGFloat heightContent = height;
//    NSArray *contentArray1 = [content componentsSeparatedByString:@"<p>"];
//    NSMutableArray *contentsMuArray = [NSMutableArray array];
//    for (NSString *subs in contentArray1) {
//        NSArray *subArray = [subs componentsSeparatedByString:@"</p>"];
//        [contentsMuArray addObjectsFromArray:subArray];
//    }
//    for (int i = 0; i < contentsMuArray.count; i++) {
//        CGFloat width = SCREENWIDTH - 20;
//        NSString *subString = contentsMuArray[i];
//        NSRange range = [subString rangeOfString:@"image_"];
//        if (range.location != NSNotFound) {
//            CGFloat timeHeight = [strContent ZSQ_heigtWithFont:[UIFont systemFontOfSize:18] with:width];
//            
//            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, heightContent, width, timeHeight)];
//            label.text = strContent;
//            
//            heightContent += timeHeight + 10;
//            NSRange endRange = [subString rangeOfString:@">"];
//            subString = [subString substringFromIndex:range.length + range.location];
//            int index = [[subString substringToIndex:endRange.location - 1] intValue];
//            if (index < self.photos.count) {
//                NSLog(@"%@",self.photos[i][@"pic"]);
//                NSString *pic = self.photos[i][@"pic"];
//                NSInteger width = [self.photos[i][@"height"] integerValue];
//                NSInteger height = [self.photos[i][@"width"] integerValue];
//                CGFloat viewHeight = height/(CGFloat)width * VIEWWIDTH;
//                UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, heightContent, width, viewHeight)];
//                [imageView sd_setImageWithURL:[NSURL URLWithString:pic] placeholderImage:nil];
//                heightContent += viewHeight;
//            }
//            NSRange aRange = [subString rangeOfString:@"<a"];
//            while (aRange.location != NSNotFound) {
//                if (aRange.location != NSNotFound) {
//                    if (aRange.location > 0) {
//                        NSString *string1 = [subString substringToIndex:aRange.location - 1];
//                        [strContent stringByAppendingString:string1];
//                    }
//                    
//                    NSString *string2 = [subString substringFromIndex:aRange.location + aRange.length];
//                    NSRange endArange = [string2 rangeOfString:@">"];
//                }
//            }
//            
//        }
//    }
//}



//- (void)addContent:(NSString *)title time:(NSString *)time content:(NSString *)content fromnews:(NSString *)fromnews {
//    CGFloat y = 0;
//    CGFloat width = SCREENWIDTH - 20;
//    CGFloat titleHeight = [title ZSQ_heigtWithFont:[UIFont systemFontOfSize:20] with:width];
//    
//    //    self.contentView
//    y += titleHeight;
//    CGFloat timeHeight = [title ZSQ_heigtWithFont:[UIFont systemFontOfSize:18] with:width];
//    y += timeHeight;
//    
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, y)];
//    label.text = [title stringByAppendingFormat:@"/n%@",time];
//    [self.scrollView addSubview:label];
//    //    [self subcontentString:content Height:y];
//    
//}





@end
